#include"../TcpServer/TcpServer.hpp"

class EchoServer{
private:
    TcpServer _server;

    void close_callback(const ConnectPtr conn)
    {
        DBG_LOG("Del connection:%p", conn.get())
    }

    void connected_callback(const ConnectPtr conn)
    {
        DBG_LOG("New connection:%p", conn.get())
    }

    void message_callback(const ConnectPtr conn, Buffer* buf)
    {
       conn->SendInLoop(buf->get_read_ptr(), buf->get_readable_size());
       buf->move_read_pos(buf->get_readable_size());
       conn->CloseInLoop();
    }
public:
    EchoServer(uint16_t port)
    :_server(port)
    {
        _server.SetThreadCount(2);
        _server.SetInactiveRelease(1);
        _server.SetCloseCallback(std::bind(&EchoServer::close_callback, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::message_callback, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetConnectedCallback(std::bind(&EchoServer::connected_callback, this, std::placeholders::_1));
    }

    void Start()
    {
        _server.Start();
    }
};